﻿package rd.hardcore.ui.pad 
{
	import aze.motion.eaze;
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	import flash.net.URLRequest;
	import fr.digitas.flowearth.conf.Conf;
	import rd.debug.Debug;
	import rd.hardcore.engine.save.SaveManager;
	import rd.hardcore.engine.tools.Tools;
	import rd.hardcore.params.GlobalParams;
	
	/**
	 * Pad display
	 * @author Romain Prache
	 */
	public class FrontPad extends ExternalDragNDrop
	{
		/* VAR */
		private var loader	: Loader;
		private var bmp		: Bitmap;
		private var inner	: Bitmap;
		private var file	: String;
		
		/* Front Pad */
		public function FrontPad() 
		{
			loader = new Loader();
			
			buttonMode		= true;
						
			addEventListener(MouseEvent.ROLL_OVER, 	handleOver);
			addEventListener(MouseEvent.ROLL_OUT, 	handleOut);
		}
		
		/* Mouse Eventz */
		private function handleOver(e:MouseEvent):void 
		{
			eaze(this).to(.5, { alpha:.9 } );
		}
		
		private function handleOut(e:MouseEvent):void 
		{
			eaze(this).to(.5, { alpha:1 } );
		}
		
		/* Thumbnail Config */
		public function setThumb( _target : String) : void
		{
			if ( _target == "" ) return; 
			
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, displayThumb);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, fail);

			var url : String = File.applicationStorageDirectory.nativePath + "/" + _target;
			var str : String = Tools.macOsUrlProofing( url );
			
			loader.load( new URLRequest( str ));
		}
		
		private function displayThumb(e:Event):void 
		{
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, displayThumb);
			loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, fail);
			
			if (bmp != null)
			{
				bmp.bitmapData.dispose();
				removeChild(bmp);
				bmp = null;
			}
			
			bmp = loader.content as Bitmap;
			bmp.smoothing = true;

			
			var _w : int = bmp.width;
			var _h : int = bmp.height;
			
			var ratioImg : Number = _w / _h;
			
			if (ratioImg < 1) {
				
				bmp.width = GlobalParams.PAD_SIZE;
				bmp.height = _h / _w * GlobalParams.PAD_SIZE;
				
			}else {
				
				bmp.height = GlobalParams.PAD_SIZE;
				bmp.width = _w / _h * GlobalParams.PAD_SIZE;
			}
						
			bmp.x = (	GlobalParams.PAD_SIZE - bmp.width	) 	>> 1;
			bmp.y = (	GlobalParams.PAD_SIZE - bmp.height	) 	>> 1;
						
			addChild(bmp);
			
			if (file == "empty.jpg") return;
			
			// inner shadow			
			var bmpd : InnerShadow_FC = new InnerShadow_FC(65, 65);
			inner = new Bitmap(bmpd);
			addChild(inner);
			
			handleOut( null );
		}
		
		private function fail(e:IOErrorEvent):void 
		{
			Debug.log("fail", Debug.RED);
		}

		
	}
	
}